package com.mc.common.gisserver;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class GisService {
    private static final Logger log = LoggerFactory.getLogger(GisService.class);

    public String restTemplate( MultiValueMap<String, String> params, String url){
        RestTemplate restTemplate = new RestTemplate();
        params.add("f","pjson");
        params.add("frollbackOnFailure", "true");
        params.add("rollbackOnFailure", "true");
        String cltTime = null;

        log.info(params.toString());
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, null);
        ResponseEntity<String> response = restTemplate.postForEntity(
                url,
                requestEntity,
                String .class);
        String jsonObject = response.getBody();
        log.info(jsonObject);
        return jsonObject;

    }

    public  String selectObjectId(MultiValueMap<String, String> params,String URL){
        RestTemplate restTemplate = new RestTemplate();
        params.add("geometryType","esriGeometryEnvelope");
        params.add("spatialRel","esriSpatialRelIntersects");
        params.add("units","esriSRUnit_Foot");
        params.add("outFields","OBJECTID");
        params.add("returnGeometry","false");

        params.add("returnDistinctValues","false");
        params.add("returnIdsOnly","false");
        params.add("returnCountOnly","false");
        params.add("returnExtentOnly","false");
        params.add("returnZ","false");
        params.add("returnM","false");
        params.add("multipatchOption","xyFootprint");
        params.add("returnTrueCurves","false");
        params.add("returnExceededLimitFeatures","false");
        params.add("returnCentroid","false");
        params.add("sqlFormat","none");
        params.add("featureEncoding","esriDefault");
        params.add("f","pjson");

        log.info("请求参数:"+params);

        String body = null;
        try {
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, null);

            ResponseEntity<String> forEntity = restTemplate.postForEntity(URL+"query", requestEntity, String.class);
            body = forEntity.getBody();
            log.info(body);
        } catch (Exception e) {
        }
        return JSONObject.parseObject(body).getJSONArray("features").getJSONObject(0).getJSONObject("attributes").getString("OBJECTID");
    }

}
